<?php

declare(strict_types=1);

namespace App\Service\Client;


use App\Constants\DefaultValue;
use App\Dao\NoticeEmployerDao;
use App\Dao\NoticeUserDao;
use App\Model\NoticeEmployer;
use App\Model\NoticeUser;
use Hyperf\Di\Annotation\Inject;
use App\Constants\Model\Notice as NoticeConstants;

class NoticeService extends BaseService
{
    /**
     * @Inject()
     * @var NoticeUserDao
     */
    protected $noticeUserDao;

    /**
     * @Inject()
     * @var NoticeEmployerDao
     */
    protected $noticeEmployerDao;


    /**
     * 用户通知未读数
     * @return array
     */
    public function userNoticeNotRead(): array
    {
        $notRead = $this->noticeUserDao
            ->where(
                [
                    'user_id' => $this->userInfo()->id,
                    'status' => NoticeConstants::STATUS_UNREAD
                ]
            )
            ->count();

        /** @var NoticeUser $newNotice */
        $newNotice = $this->noticeUserDao
            ->where(
                [
                    'user_id' => $this->userInfo()->id,
                    'status' => NoticeConstants::STATUS_UNREAD
                ]
            )
            ->orderBy('created_at', 'desc')
            ->first();
        if ($newNotice) {
            $newNotice->created_time = $this->renderCreateTime((string)$newNotice->created_at);
        }

        return [
            'notRead' => $notRead,
            'newNotice' => $newNotice,
        ];
    }


    /**
     * 用户通知列表
     * @param array $data
     * @return array
     */
    public function userNotice(array $data): array
    {
        //将所有未读置为已读
        $this->noticeUserDao
            ->where(
                [
                    'user_id' => $this->userInfo()->id,
                    'status' => NoticeConstants::STATUS_UNREAD
                ]
            )
            ->update(['status' => NoticeConstants::STATUS_READ]);

        $perPage = isset($data['perPage']) ? (int)$data['perPage'] : DefaultValue::PER_PAGE_DEFAULT;

        $page = $this->noticeUserDao->paginate(
            ['user_id' => $this->userInfo()->id],
            $perPage,
            ['*'],
            ['orderBy' => ['created_at', 'desc']]
        );

        return [
            'perPage' => $page->perPage(),//每页条数
            'currentPage' => $page->currentPage(),//当前页
            'total' => $page->total(),//数据总数
            'hasMorePages' => $page->hasMorePages(),//是否还有更多页
            'data' => $page->items(),//数据
        ];
    }

    /**
     * 悬赏主通知未读数
     * @return array
     */
    public function employerNoticeNotRead()
    {
        $employerIds = $this->userIdsInCooperatives();

        $notRead = $this->noticeEmployerDao
            ->where(
                [
                    'employer_ids' => $employerIds,
                    'status' => NoticeConstants::STATUS_UNREAD

                ]
            )
            ->count();

        /** @var NoticeEmployer $newNotice */
        $newNotice = $this->noticeEmployerDao
            ->where(
                [
                    'employer_ids' => $employerIds,
                    'status' => NoticeConstants::STATUS_UNREAD
                ]
            )
            ->orderBy('created_at', 'desc')
            ->first();
        if ($newNotice) {
            $newNotice->created_time = $this->renderCreateTime((string)$newNotice->created_at);
        }

        return [
            'notRead' => $notRead,
            'newNotice' => $newNotice,
        ];
    }

    /**
     * 悬赏主通知列表
     * @param array $data
     * @return array
     */
    public function employerNotice(array $data): array
    {
        $employerIds = $this->userIdsInCooperatives();

        //将所有未读置为已读
        $this->noticeEmployerDao
            ->where(
                [
                    'employer_ids' => $employerIds,
                    'status' => NoticeConstants::STATUS_UNREAD
                ]
            )
            ->update(['status' => NoticeConstants::STATUS_READ]);

        $perPage = isset($data['perPage']) ? (int)$data['perPage'] : DefaultValue::PER_PAGE_DEFAULT;

        $page = $this->noticeEmployerDao->paginate(
            ['employer_ids' => $employerIds],
            $perPage,
            ['*'],
            ['orderBy' => ['created_at', 'desc']]
        );

        return [
            'perPage' => $page->perPage(),//每页条数
            'currentPage' => $page->currentPage(),//当前页
            'total' => $page->total(),//数据总数
            'hasMorePages' => $page->hasMorePages(),//是否还有更多页
            'data' => $page->items(),//数据
        ];
    }


    protected function renderCreateTime(string $time)
    {
        if (!$time) {
            return '';
        }

        $result = '';
        $nowTime = time();
        $timestamp = strtotime($time);

        $tmpYear = date('Y', $timestamp);
        $tmpDate = date('m/d', $timestamp);
        $tmpTime = date('H:m', $timestamp);

        $nowYear = date('Y', $nowTime);
        $nowDate = date('m/d', $nowTime);

        if ($tmpYear != $nowYear) {
            $result .= $tmpYear;
        }

        if ($tmpDate != $nowDate) {
            if (strlen($result) > 0) {
                $result .= '/';
            }
            $result .= $tmpDate . ' ';
        }

        $result .= $tmpTime;

        return $result;
    }


}